//
//  HEDocOrderTransFeesCell.m
//  HealthElderly
//
//  Created by 吴欧 on 2021/6/24.
//  Copyright © 2021 honglutec. All rights reserved.
//

#import "HEDocOrderTransFeesCell.h"

@interface HEDocOrderTransFeesCell ()

@property (nonatomic, strong) UILabel *titleLb;
@property (nonatomic, strong) UILabel *transFeeLb;

@property (nonatomic, assign) BOOL hasCreate;
@property (nonatomic, strong) NSMutableArray *itemArray;
@property (nonatomic, strong) NSMutableArray *itemValArray;

@end

@implementation HEDocOrderTransFeesCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
    }
    return self;
}

- (void)initUI {

    _itemArray = @[].mutableCopy;
    _itemValArray = @[].mutableCopy;

    _titleLb = [UILabel new];
    _titleLb.text = @"交通费用：";
    _titleLb.font = DYBlodFont(18);
    [self.contentView addSubview:_titleLb];
    
    _transFeeLb = [UILabel new];
    _transFeeLb.text = @"-";
    _transFeeLb.font = DYBlodFont(18);
    _transFeeLb.textColor = CPriceColor;
    _transFeeLb.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:_transFeeLb];
 
    [_titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(15);
        make.left.equalTo(self.contentView).mas_offset(15);
        make.right.equalTo(_transFeeLb.mas_left).mas_offset(-15);
    }];

    [_transFeeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(15);
        make.right.equalTo(self.contentView).mas_offset(-15);
        make.left.equalTo(_titleLb.mas_right).mas_offset(-15);
    }];
    
    [_titleLb setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    
    [self createItemViews];

}

- (void)refreshContent:(GDFiterCellModel *)fiterModel formModel:(GDFormModel *)formModel indexPath:(NSIndexPath *)indexPath {
    _transFeeLb.text = [NSString stringWithFormat:@"￥%@", nullToDef([self.formModel.od_transFee.transFees keepTwo])];
    [self assignItemVal];
}

- (void)configCellWithModel:(id)model fiterModel:(GDFiterCellModel *)fiterModel indexPath:(NSIndexPath *)indexPath {
    _transFeeLb.text = [NSString stringWithFormat:@"￥%@", nullToDef([self.formModel.od_transFee.transFees keepTwo])];
    [self assignItemVal];
}

- (void)assignItemVal {
    _itemValArray = @[
        [NSString stringWithFormat:@"%@公里", nullToDef([self.formModel.od_transFee.distance keepTwo])],
        [NSString stringWithFormat:@"(%@-%@)*%@*2 = %@"
                                 , nullToDef([self.formModel.od_transFee.distance keepTwo])
                                 , nullToDef(self.formModel.od_transFee.freeDistance)
                                 , nullToDef(self.formModel.od_transFee.perFee)
                                 , nullToDef([self.formModel.od_transFee.singleTransFees keepTwo])
                                 ],
        nullToDef(self.formModel.od_transFee.serverCnt),
        nullToDef([self.formModel.od_transFee.transFees keepTwo]),
        [NSString stringWithFormat:@"交通费%@公里内免费，超出部分按%@元/公里计算", nullToDef(self.formModel.od_transFee.freeDistance), nullToDef(self.formModel.od_transFee.perFee)]
    ].mutableCopy;
    
    [_itemArray enumerateObjectsUsingBlock:^(UILabel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.text = _itemValArray[idx];
    }];
    // 重点
    [self.contentView.superview layoutIfNeeded];
}

// 只能先创建视图 再赋值
- (void)createItemViews {
    
    NSMutableArray *smallArray = @[].mutableCopy;

    NSArray *items = @[@{@"title":@"路程距离：", @"value": @"-"},
                       @{@"title":@"单次交通费：", @"value": @"-"},
                       @{@"title":@"总服务次数：", @"value": @"-"},
                       @{@"title":@"总交通费：", @"value": @"-"},
                       @{@"title":@"备注：", @"value": @"-"},
                    ];

    [items enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

        UIView *smallItem = [self createItemViewWithModel:obj];
        [self.contentView addSubview: smallItem];

        [smallArray addObject:smallItem];
        [smallItem mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).mas_offset(15);
            make.right.equalTo(self.contentView).mas_offset(-15);

            if (idx == 0) {
                make.top.equalTo(_titleLb.mas_bottom).offset(10);
            }else {
                UIView *lastView = smallArray[idx-1];
                make.top.equalTo(lastView.mas_bottom).offset(8);
            }

            if (idx == (items.count-1)) {
                make.bottom.equalTo(self.contentView).offset(-10);
            }
        }];
    }];
}

// 创建 小项视图
- (UIView *)createItemViewWithModel:(NSDictionary *)model{
    UIView *itemWarp = [UIView new];

    // middle
    UILabel *nameTitlLb = [UILabel new];
    nameTitlLb.text = model[@"title"];
    nameTitlLb.font = DYFont(16);
    nameTitlLb.textColor = CNavLightBlackColor;
    [itemWarp addSubview: nameTitlLb];

    // middle
    UILabel *nameLb = [UILabel new];
    nameLb.text = model[@"value"];
    nameLb.font = DYFont(16);
    nameLb.textColor = CNavLightBlackColor;
    nameLb.numberOfLines = 0;
    nameLb.textAlignment = NSTextAlignmentRight;
    if ([model containsObjectForKey:@"textColor"]) {
        nameLb.textColor = model[@"textColor"];
    }
    [itemWarp addSubview: nameLb];
    [_itemArray addObject: nameLb];

    [nameTitlLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(itemWarp);
        make.left.equalTo(itemWarp);
        make.right.equalTo(nameLb.mas_left).offset(-5);
    }];
    [nameTitlLb setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [nameLb setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];

    [nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(itemWarp);
        make.left.equalTo(nameTitlLb.mas_right).offset(3);
        make.right.bottom.equalTo(itemWarp);
    }];

    return itemWarp;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
